const { exchange } = require('./index')
const moment = require('moment')
const chalk = require('chalk')
const Decimal = require('decimal.js')
const {
  KlineService: {
    isRed,
    isGreen,
    logRG
  }
} = require('./../service/index')
const log = console.log
const main = async () => {
  const symbol = 'BTC/USDT'
  // 获取到k线
  const klineList = await exchange.fetchOHLCV(symbol, '3m')
  // 格式化k线
  const formatedKlineList = klineList.map(item => ({
    time: moment(item[0]).format(),
    openPrice: item[1],
    highestPrice: item[2],
    lowestPrice: item[3],
    closePrice: item[4],
    volumn: item[5],
    middilePrice: Decimal(item[1]).add(item[4]).div(2).toNumber(),
    color: item[1] > item[4] ? 'red': 'green'
  }))

  // 按颜色打印出来
  formatedKlineList.forEach((item, index) => {
    const next1 = formatedKlineList[index + 1]
    const next2 = formatedKlineList[index + 2]
    const next3 = formatedKlineList[index + 3]
    const next4 = formatedKlineList[index + 4]
    const next5 = formatedKlineList[index + 5]
    const next6 = formatedKlineList[index + 6]
    const next7 = formatedKlineList[index + 7]
    const next8 = formatedKlineList[index + 8]
    // if (item.volumn > 250 && next1 && next2 && next3) {
    //   log(
    //     item.time,
    //     chalk[item.color](item.middilePrice),
    //     chalk[next1.color](next1.middilePrice),
    //     chalk[next2.color](next2.middilePrice),
    //     chalk[next3.color](next3.middilePrice)
    //   )
    //   log('')
    // }
    if (next1 && next2 && next3 && next4 && next8) {
      if (isRed(item) && isRed(next1) && isRed(next2) && isRed(next3) && isRed(next4)) {
        // log(
        //   item.time,
        //   chalk[item.color](item.middilePrice),
        //   chalk[next1.color](next1.middilePrice),
        //   chalk[next2.color](next2.middilePrice),
        //   chalk[next3.color](next3.middilePrice),
        //   chalk[next4.color](next4.middilePrice),
        //   chalk[next5.color](next5.middilePrice),
        //   chalk[next6.color](next6.middilePrice),
        //   chalk[next7.color](next7.middilePrice),
        //   chalk[next8.color](next8.middilePrice)
        // )
        log(
          item.time,
          chalk[item.color](item.middilePrice),
          chalk[next1.color](next1.middilePrice - item.middilePrice),
          chalk[next2.color](next2.middilePrice - next1.middilePrice),
          chalk[next3.color](next3.middilePrice - next2.middilePrice),
          chalk[next4.color](next4.middilePrice - next3.middilePrice),
          chalk[next5.color](next5.middilePrice - next4.middilePrice),
          chalk[next6.color](next6.middilePrice - next5.middilePrice),
          chalk[next7.color](next7.middilePrice - next6.middilePrice),
          chalk[next8.color](next8.middilePrice - next7.middilePrice)
        )
      }
      if (isGreen(item) && isGreen(next1) && isGreen(next2) && isGreen(next3) && isGreen(next4)) {
        // log(
        //   item.time,
        //   chalk[item.color](item.middilePrice),
        //   chalk[next1.color](next1.middilePrice),
        //   chalk[next2.color](next2.middilePrice),
        //   chalk[next3.color](next3.middilePrice),
        //   chalk[next4.color](next4.middilePrice),
        //   chalk[next5.color](next5.middilePrice),
        //   chalk[next6.color](next6.middilePrice),
        //   chalk[next7.color](next7.middilePrice),
        //   chalk[next8.color](next8.middilePrice)
        // )
        log(
          item.time,
          chalk[item.color](item.middilePrice),
          chalk[next1.color](next1.middilePrice - item.middilePrice),
          chalk[next2.color](next2.middilePrice - next1.middilePrice),
          chalk[next3.color](next3.middilePrice - next2.middilePrice),
          chalk[next4.color](next4.middilePrice - next3.middilePrice),
          chalk[next5.color](next5.middilePrice - next4.middilePrice),
          chalk[next6.color](next6.middilePrice - next5.middilePrice),
          chalk[next7.color](next7.middilePrice - next6.middilePrice),
          chalk[next8.color](next8.middilePrice - next7.middilePrice)
        )
      }
    }
    // log(chalk[item.color](item.volumn))
    // log(chalk[item.color](item.volumn))
  })

  // log(formatedKlineList.filter(item => item.volumn > 250).length)
  
  /* 红绿绿 */
  let count_0_1_1 = 0
  // 红绿绿后面是红
  let count_0_after_0_1_1 = 0
  // 红绿绿后面是绿
  let count_1_after_0_1_1 = 0

  /* 红红绿绿 */
  let count_0_0_1_1 = 0
  // 红红绿绿后面是红
  let count_0_after_0_0_1_1 = 0
  // 红红绿绿后面是绿
  let count_1_after_0_0_1_1 = 0

  /* 绿红红 */
  let count_1_0_0 = 0
  // 绿红红后面有多少个红
  let count_0_after_1_0_0 = 0
  // 绿红红后面有多少个绿
  let count_1_after_1_0_0 = 0

  /* 绿绿红红 */
  let count_1_1_0_0 = 0
  // 绿绿红红后面是红
  let count_0_after_1_1_0_0 = 0
  // 绿绿红红后面是绿
  let count_1_after_1_1_0_0 = 0

  /* 绿红红红 */
  let count_1_0_0_0 = 0
  // 绿红红红后面有多少个红
  let count_0_after_1_0_0_0 = 0
  // 绿红红红后面有多少个绿
  let count_1_after_1_0_0_0 = 0

  /* 红绿绿绿 */
  let count_0_1_1_1 = 0
  // 红绿绿绿后面是红
  let count_0_after_0_1_1_1 = 0
  // 红绿绿绿后面是绿
  let count_1_after_0_1_1_1 = 0

  formatedKlineList.forEach((item, index) => {
    // 从第3个开始
    if (index > 2) {
      const _0 = formatedKlineList[index - 3]
      const _1 = formatedKlineList[index - 2]
      const _2 = formatedKlineList[index - 1]
      const _3 = item
      const _4 = formatedKlineList[index + 1]
      // 红绿绿
      if (isRed(_1) && isGreen(_2) && isGreen(_3)) {
        count_0_1_1++
        if (isRed(_4)) count_0_after_0_1_1++
        if (isGreen(_4)) count_1_after_0_1_1++
        // 红红绿绿
        if (isRed(_0)) {
          count_0_0_1_1++
          if (isRed(_4)) count_0_after_0_0_1_1++
          if (isGreen(_4)) count_1_after_0_0_1_1++
        }
      }

      // 绿红红
      if (isGreen(_1) && isRed(_2) && isRed(_3)) {
        count_1_0_0++
        if (isRed(_4)) count_0_after_1_0_0++
        if (isGreen(_4)) count_1_after_1_0_0++
        // 绿绿红红
        if (isRed(_0)) {
          count_1_1_0_0++
          if (isRed(_4)) count_0_after_1_1_0_0++
          if (isGreen(_4)) count_1_after_1_1_0_0++
        }
      }
    }

    // 从第4个开始
    if (index > 3) {
      const _0 = formatedKlineList[index - 4]
      const _1 = formatedKlineList[index - 3]
      const _2 = formatedKlineList[index - 2]
      const _3 = formatedKlineList[index - 1]
      const _4 = item
      // 红绿绿绿
      if (isRed(_0) && isGreen(_1) && isGreen(_2) && isGreen(_3)) {
        count_0_1_1_1++
        if (isRed(_4)) count_0_after_0_1_1_1++
        if (isGreen(_4)) count_1_after_0_1_1_1++
      }

      // 绿红红红
      if (isGreen(_0) && isRed(_1) && isRed(_2) && isRed(_3)) {
        count_1_0_0_0++
        if (isRed(_4)) count_0_after_1_0_0_0++
        if (isGreen(_4)) count_1_after_1_0_0_0++
      }
    }
  })

  // logRG(`红绿绿共有${count_0_1_1}个，${count_0_after_0_1_1}个红，${count_1_after_0_1_1}个绿`)

  // logRG(`绿红红共有${count_1_0_0}个，后面有${count_1_after_1_0_0}个绿，${count_0_after_1_0_0}个红`)


  // logRG(`红红绿绿共有${count_0_0_1_1}个，${count_0_after_0_0_1_1}个红，${count_1_after_0_0_1_1}个绿`)

  // logRG(`绿绿红红共有${count_1_1_0_0}个，后面有${count_1_after_1_1_0_0}个绿，${count_0_after_1_1_0_0}个红`)

  // logRG(`红绿绿绿共有${count_0_1_1_1}个，${count_0_after_0_1_1_1}个红，${count_1_after_0_1_1_1}个绿`)

  // logRG(`绿红红红共有${count_1_0_0_0}个，后面有${count_0_after_1_0_0_0}个绿，${count_1_after_1_0_0_0}个红`)

}
// const testRedGreen = (list, rgList) => {
//   let count = 0,
//   count_0_after = 0,
//   count_1_after = 0

//   list.forEach((item, listIndex) => {
//     if (listIndex > rgList.length - 1) {
//       if (rgList.reduce((result, item) => {
//         let fnIsColor = item === 0 ? isRed : isGreen
//         return result && fnIsColor(item)
//       }, true)) {
//         count++
//         if ()
//       }
//     }
//   })
// }
main()


process.on('uncaughtException', e => {
  console.log(e)
  process.exit(1)
})

process.on('unhandledRejection', function (err) {
  console.log(err)
  process.exit(1)
  //监听Promise没有被捕获的失败函数
})
